/**
 * @author Juntalis
 */

/**
 * User interface object.
 */
var UI = {

	/**
	 * Gets the links in the header.
	 * @return {Array}	An array of of all current links in the header.
	 */
	Links: function() {
			return $("#HdTop > a").toArray().reverse();
	},
		
	/**
	 * Add a link to the header. (Appears on the left.)
	 * @param {String}	sUrl	Can be an external link or an archor (Written as #blahblah). To use javascript, use # as the url and bind an click event to the link using $("#" + id).click(function(){ yourshirthere });
	 * @param {String}	sText	The text shown for the link. Cannot contain html of any kind. Will return null immediately if fed a <, >, or " character.
	 * @param {Function}	cbDone	A function to execute after the link is loaded.
	 * @return {String}	The ID of the new link or null on failure. Note: If you want to store the id of the link for later use, try using the JQuery data function or something.
	 */
	AddLink: function(sUrl,sText,cbDone) {
		if ((!sUrl.match(/^(?:https?:\/\/|ftp:\/\/|\#).*$/i)) || (sUrl.match(/["><']/i))) return null;
		if (sText.match(/<\/?[a-z][a-z0-9]*[^<>]*>/i)) return null;
		var rId = "RndId_" + Misc.UniqueID(15);
		$("#HdTop").prepend("<a href=\"" + sUrl + "\" id=\"" + rId + "\" style=\"display:none\">" + sText + "</a>");
		$("#HdTop > #" + rId).stop().fadeIn(400, function() {
			var _sHref = $(this).attr("href");
			if (_sHref.match(/^(?:https?:\/\/|ftp:\/\/).*$/i)) {
				$.data(this, "_url", _sHref);
				$(this).attr("href","#").click(function(){ Misc.OpenUrl($.data(this,"_url")); });
			}
			$("<span> </span>").delay(75).insertAfter(this).delay(75).append("|").delay(75).append(" ");
			if (cbDone) cbDone();
		});
		return rId;
	},
		
	/**
	 * Remove a link by ID or by index. For this particular function, the links are indexed in reverse. This means that the Reversing Juntalis is link 0, and any others are 1,2,3 etc from right to left.
	 * @param {String,Integer}	varTarget Can be the string id of the link, or an integer index. Cannot be TBHAuthorWeb or 0, as those are the linkback to Junt's website.
	 */
	RemoveLink: function(varTarget) {
		if (Misc.isNumeric(varTarget)) {
			if((varTarget <= 0) || (varTarget >= ($("#HdTop > a").length))) return;
			$("#HdTop > a:eq(" + ($("#HdTop > a").length - varTarget - 1) + ")").stop().fadeOut(400, function() {
				var txtBtw = "#HdTop > span:eq(" + ($("#HdTop > span").length - varTarget) + ")";
				$(txtBtw)
					.delay(75).html($(txtBtw).html().substring(1))
					.delay(75).html($(txtBtw).html().substring(1))
					.delay(75).remove();
				$(this).remove();
				}
			);
		} else {
			if((varTarget == "TBHAuthorWeb") || (!$("#HdTop > #" + varTarget).length)) return;
			$("#HdTop > #" + varTarget).stop().fadeOut(400, function() {
				var txtBtw = $(this).next("span");
				$(txtBtw)
					.delay(75).html($(txtBtw).html().substring(1))
					.delay(75).html($(txtBtw).html().substring(1))
					.delay(75).html("");
				$(txtBtw).remove();
				$(this).remove();
			});
		}
	},
		
	/**
	 * Manages the current view (Head text, Title text, and main content.)
	 * @return {UiManager}
	 */
	Manager : new UiManager()
};


// Determines what's shown in the middle area.
function UiManager()
{
	// Internal use stuff.
	var sTitle;
	var sContent;
	var _blVisible = false;
	
	// The title that will be displayed.
	this.Title = function(HTitle) {
		if (!HTitle) return sTitle;
		else sTitle = HTitle;
	}
	
	// Get or set the content
	this.Content = function(ctContent) {
		if (!ctContent) return sContent;
		else sContent = ctContent;
	}
	
	// Handles most of the work of transitioning from one interface
	// to another.
	this.LoadInterface = function(objUi) {
		if (!_blVisible) {
			sTitle = objUi.Title;
			sContent = objUi.Body;
			this._toggleShow(objUi.onLoad, objUi.Height);
			objUi.onBuild();
		} else {
			this._toggleShow(
				function() {
					sTitle = objUi.Title;
					sContent = objUi.Body;
					UI.Manager._toggleShow(objUi.onLoad, objUi.Height);
					objUi.onBuild();
				}
			);
		}
	}
		
	// Changes the title to str in an animated fashion.
	this.ChangeTitle = function(str, callback) {
		if ($("head > title").html() == "Toribash -&gt; HTML")
			this._toggleTitle(str, callback);
		else 
			this._toggleTitle(null,function(){UI.Manager._toggleTitle(str,callback);});
	}
		
	// [PRIVATE METHOD]
	// Toggles the title in an animated way.
	this._toggleTitle = function(str,cb) {
		// If we have section-specific title, we're changing back to normal.
		if ($("head > title").html() != "Toribash -&gt; HTML") {
		
			// Fade out the header title.
			$("#HTitle").animate({opacity:"0.0"},400, function() {
				$(this).html("");
				if(cb) cb();
			});
			
			// Work on the page title.
			do {
				$("head > title").delay(50).html($("head > title").html().substring(1));
			} while ($("head > title").html() != "Toribash -&gt; HTML");
		} else {
			// Change the page title.
			if (str) sTitle = str;
			
			// Insert the new/old title.
			$("#HTitle").html(sTitle);
			
			// Fade in header.
			$("#HTitle").animate({opacity:"1.0"},400);
			
			// Assemble page title.
			var sPageTitle = sTitle + " | ";
			for (var i = 0; i <= sTitle.length + 2; i++) {
				$("head > title").delay(50).html(sPageTitle.charAt(sPageTitle.length - 1 - i) + $("head > title").html());
			}
		}
	}

	// [PRIVATE METHOD]
	// Slide and fade in or out the content,
	// depending on its current state.
	// cbDone is the callback for when
	// the box finishes.
	this._toggleShow = function(cbdone, iheight) {
		if (!_blVisible) {
			$("#CtMain")
				.html(sContent)
				.animate(
					{
						height : iheight + "px",
						opacity : "1.0"	
					},
					600,
					function() {
						_blVisible = true;
						if (cbdone) cbdone();
					}
				);
			this._toggleTitle();
		} else {
			$("#CtMain")
				.animate(
					{
						height : "0px",
						opacity : "0.0"
					},
					600,
					function() {
						$("#CtMain").html("");
						_blVisible = false;
						if (cbdone) cbdone();
					}
				);
			this._toggleTitle();
		}
	}
}